using AntdUI;
using System;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
using TangCaptureTraffic.Properties;
using System.ComponentModel;

namespace TangCaptureTraffic.Forms
{
    /// <summary>
    /// 启动窗体类，显示应用程序启动过程中的加载界面
    /// </summary>
    public class SplashForm : AntdUI.Window
    {
        private AntdUI.Progress progress;
        private AntdUI.Label titleLabel;
        private AntdUI.Label versionLabel;
        private AntdUI.Label statusLabel;

        /// <summary>
        /// 当前加载状态
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        /// <summary>
        /// 当前加载状态
        /// </summary>
        public string Status
        {
            get => statusLabel.Text;
            set => statusLabel.Text = value;
        }

        /// <summary>
        /// 当前进度值(0-100)
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        /// <summary>
        /// 当前进度值(0-100)
        /// </summary>
        public float ProgressValue
        {
            get => progress.Value;
            set => progress.Value = value;
        }

        public SplashForm()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.ShowInTaskbar = false;
            this.FormBorderStyle = FormBorderStyle.None;
            this.Size = new Size(400, 300);
            
            // 设置窗体为模态对话框
            this.ControlBox = false;
        }

        private void InitializeComponent()
        {
            // 标题标签
            titleLabel = new AntdUI.Label
            {
                Text = "Tang Capture Traffic",
                Font = new Font("Microsoft YaHei", 18, FontStyle.Bold),
                ForeColor = Color.FromArgb(22, 119, 255),
                TextAlign = ContentAlignment.MiddleCenter,
                Dock = DockStyle.None,
                AutoSize = false,
                Size = new Size(400, 40),
                Location = new Point(0, 80)
            };

            // 版本标签
            versionLabel = new AntdUI.Label
            {
                Text = $"Version {Application.ProductVersion}",
                Font = new Font("Microsoft YaHei", 9),
                ForeColor = Color.Gray,
                TextAlign = ContentAlignment.MiddleCenter,
                Dock = DockStyle.None,
                AutoSize = false,
                Size = new Size(400, 20),
                Location = new Point(0, 120)
            };

            // 状态标签
            statusLabel = new AntdUI.Label
            {
                Text = "正在初始化...",
                Font = new Font("Microsoft YaHei", 9),
                ForeColor = Color.DimGray,
                TextAlign = ContentAlignment.MiddleCenter,
                Dock = DockStyle.None,
                AutoSize = false,
                Size = new Size(400, 20),
                Location = new Point(0, 200)
            };

            // 进度条
            progress = new AntdUI.Progress
            {
                Value = 0,
                Size = new Size(300, 6),
                Location = new Point(50, 230),
            };

           

            // 添加控件到窗体
            this.Controls.Add(titleLabel);
            this.Controls.Add(versionLabel);
            this.Controls.Add(statusLabel);
            this.Controls.Add(progress);
        }

        /// <summary>
        /// 更新加载状态和进度
        /// </summary>
        /// <param name="status">状态描述</param>
        /// <param name="progressValue">进度值(0-100)</param>
        public void UpdateStatus(string status, int progressValue)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(() => UpdateStatus(status, progressValue)));
                return;
            }

            this.Status = status;
            this.ProgressValue = progressValue;
            Application.DoEvents(); // 确保UI更新
        }

        /// <summary>
        /// 设置完成状态并关闭窗体
        /// </summary>
        public async Task SetCompleteAndClose()
        {
            // 完成进度
            UpdateStatus("加载完成", 100);
            await Task.Delay(500); // 短暂延迟以显示完成状态
            
            // 设置对话框结果为OK并关闭窗体
            this.DialogResult = DialogResult.OK;
        }
    }
}